﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Drawing.Imaging;
using System.Drawing.Printing;

using Model;

public partial class frmDiagnosis : BaseWinFrom
{

    Diagnosis diagnosis;

    private frmDiagnosis()
    {
        InitializeComponent();
        if (diagnosis != null && diagnosis.Status != 1)
        {
            btOK.Enabled = false;
        }
        new TextBoxBindMonthCalendar(this.txtBirthDay);
    }

    public frmDiagnosis(string DiagnosisId)
        : this(new RST.Diagnosis().Single(new Diagnosis() { Id = DiagnosisId }))
    { }

    public frmDiagnosis(Diagnosis dg)
        : this()
    {
        diagnosis = dg;
        new frmDrugsHelper(txtDrugs, dg);
        if (dg != null)
        {
            if (dg.DiagnosisUserBase != null)
            {
                txtUserName.Text = dg.DiagnosisUserBase.Name;
                txtTelephone.Text = dg.DiagnosisUserBase.Mobile;
                txtEmail.Text = dg.DiagnosisUserBase.Email;
                txtBirthDay.Text = dg.DiagnosisUserBase.BirthDay.ToString("yyyy-MM-dd");
                txtPeople.Text = dg.DiagnosisUserBase.People;
                if (dg.DiagnosisUserBase.UserSex)
                {
                    rdbMale.Checked = true;
                }
                else
                {
                    rdbFemale.Checked = true;
                }
            }
            txtProperty_Ask.Text = dg.Property_Ask;
            txtProperty_Listent.Text = dg.Property_Listen;
            txtProperty_Look.Text = dg.Property_Look;
            txtProperty_Thouch.Text = dg.Property_Touch;
            txtRemarks.Text = dg.Remarks;
            txtReseaon.Text = dg.Reseaon;
            txtMassage.Text = dg.Massage;
            if (dg.DiagnosisDrugs != null)
            {
                StringBuilder ddsb = null;
                foreach (Model.DiagnosisDrugs i in dg.DiagnosisDrugs)
                {

                    if (ddsb == null)
                        ddsb = new StringBuilder();
                    else
                        ddsb.Append(Model.DiagnosisDrugs.TAGSplitPairsGrounp);
                    ddsb.Append(i.ToString());
                }
                if (ddsb != null)
                {
                    ddsb.Append(Model.DiagnosisDrugs.TAGEND);
                    txtDrugs.Text = ddsb.ToString();
                    txtDrugs.Tag = dg.DiagnosisDrugs;
                }
            }
        }
    }

    private void btOK_Click(object sender, EventArgs e)
    {
        Save();
        this.Close();
    }

    void Save()
    {
        DateTime BirthDay;
        if (!DateTime.TryParse(txtBirthDay.Text, out BirthDay))
        {
            BirthDay = DateTime.Now;
        }
        if (diagnosis == null)
            return;

        if (diagnosis.DiagnosisUserBase == null)
            diagnosis.DiagnosisUserBase = new DiagnosisUseBase();
        diagnosis.DiagnosisUserBase.Name = txtUserName.Text;
        diagnosis.DiagnosisUserBase.BirthDay = BirthDay;
        diagnosis.DiagnosisUserBase.Email = txtEmail.Text;
        if (rdbMale.Checked)
        {
            diagnosis.DiagnosisUserBase.UserSex = true;
        }
        if (rdbFemale.Checked)
        {
            diagnosis.DiagnosisUserBase.UserSex = false;
        }
        diagnosis.DiagnosisUserBase.Mobile = txtTelephone.Text;
        diagnosis.DiagnosisUserBase.People = txtPeople.Text;

        diagnosis.Property_Ask = txtProperty_Ask.Text;
        diagnosis.Property_Listen = txtProperty_Listent.Text;
        diagnosis.Property_Look = txtProperty_Look.Text;
        diagnosis.Property_Touch = txtProperty_Thouch.Text;
        diagnosis.Reseaon = txtReseaon.Text;
        diagnosis.Remarks = txtRemarks.Text;
        diagnosis.Massage = txtMassage.Text;

        if (string.IsNullOrEmpty(diagnosis.Id))
        {
            diagnosis.DoctorId = Core.Session.Certificate.Id;
            new RST.Diagnosis().Insert(diagnosis);
        }
        else
        {
            new RST.Diagnosis().Update(diagnosis);
        }
    }

    List<DiagnosisDrugs> DiagnosisDrugs
    {
        get
        {
            return txtDrugs.Tag as List<DiagnosisDrugs>;
        }
        set
        {
        }
    }

    private void mcd_DateSelected(object sender, DateRangeEventArgs e)
    {
        txtBirthDay.Text = e.Start.ToString("yyyy-MM-dd 00:00:00");
        ((Control)sender).Visible = false;
    }

    private void txtBirthDay_Click(object sender, EventArgs e)
    {
    }

    private void txtBirthDay_Enter(object sender, EventArgs e)
    {

    }

    private void btPrint_Click(object sender, EventArgs e)
    {
        if (diagnosis == null)
        {
            if (MessageBox.Show("为保存不能打印，是否保存？", "确定", MessageBoxButtons.YesNo) == DialogResult.Yes)
            {
                Save();
            }
            else
            {
                return;
            }
        }
        if (printDialog.ShowDialog(this) != DialogResult.OK)
            return;

        if (printDialog.Document == null)
        {
            printDialog.Document = new System.Drawing.Printing.PrintDocument();
            printDialog.Document.PrintPage += new PrintPageEventHandler(Document_PrintPage);
        }
        try
        {
            printDialog.Document.Print();
        }
        catch (Exception ex)
        {
            MessageBox.Show(ex.Message, "错误", MessageBoxButtons.OK, MessageBoxIcon.Warning);
        }
    }

    void Document_PrintPage(object sender, PaintEventArgs e)
    {
        Document_PrintPage(e.Graphics);
    }

    void Document_PrintPage(object sender, PrintPageEventArgs e)
    {

        Document_PrintPage(e.Graphics);
    }

    void Document_PrintPage(Graphics g)
    {
        g.TranslateTransform(90, 60);
        float DocumentHeight = 1000;
        float DocumentWidth = 600;
        float DocumentPadding = 3;
        Font fontLabel = new Font("宋体", 14f, FontStyle.Bold);
        Font fontText = new Font("宋体", 12f, FontStyle.Regular);

        SolidBrush brushLabel = new SolidBrush(Color.Black);
        SolidBrush brushText = new SolidBrush(Color.Black);

        Pen penBorder = new Pen(Color.Black);



        g.DrawRectangle(penBorder, 0, 0, DocumentWidth, DocumentHeight);

        const string MeasureChar = " ";

        float LineHeight = Math.Max(g.MeasureString(MeasureChar, fontLabel).Height, g.MeasureString(MeasureChar, fontText).Height) + DocumentPadding * 2;
        float LineJudgHeight = Math.Abs(g.MeasureString(MeasureChar, fontLabel).Height - g.MeasureString(MeasureChar, fontText).Height);

        g.DrawString("康芝堂中医调理会馆", fontLabel, brushLabel, DocumentPadding, DocumentPadding);
        g.DrawString("电话:", fontLabel, brushLabel, DocumentPadding + DocumentWidth * 3 / 5, DocumentPadding);
        g.DrawString(diagnosis.DiagnosisUserBase.Mobile, fontText, brushText, DocumentPadding + DocumentWidth * 3 / 5 + g.MeasureString("电话:", fontText).Width, DocumentPadding + LineJudgHeight);

        g.TranslateTransform(0, LineHeight);
        g.DrawLine(penBorder, 0, 0, DocumentWidth, 0);

        g.DrawString("姓名", fontLabel, brushLabel, DocumentPadding + DocumentWidth / 5 * 0, DocumentPadding);

        g.DrawLine(penBorder, DocumentWidth / 5 * 1, 0, DocumentWidth / 5 * 1, LineHeight);

        g.DrawString("性别", fontLabel, brushLabel, DocumentPadding + DocumentWidth / 5 * 1, DocumentPadding);

        g.DrawLine(penBorder, DocumentWidth / 5 * 2, 0, DocumentWidth / 5 * 2, LineHeight);

        g.DrawString("名族", fontLabel, brushLabel, DocumentPadding + DocumentWidth / 5 * 2, DocumentPadding);

        g.DrawLine(penBorder, DocumentWidth / 5 * 3, 0, DocumentWidth / 5 * 3, LineHeight);

        g.DrawString("年龄", fontLabel, brushLabel, DocumentPadding + DocumentWidth / 5 * 3, DocumentPadding);

        g.DrawLine(penBorder, DocumentWidth / 5 * 4, 0, DocumentWidth / 5 * 4, LineHeight);

        g.DrawString("来诊日期", fontLabel, brushLabel, DocumentPadding + DocumentWidth / 5 * 4, DocumentPadding);

        g.TranslateTransform(0, LineHeight);
        g.DrawLine(penBorder, 0, 0, DocumentWidth, 0);

        g.DrawString(diagnosis.DiagnosisUserBase.Name, fontText, brushText, DocumentPadding + DocumentWidth / 5 * 0, DocumentPadding + LineJudgHeight);

        g.DrawLine(penBorder, DocumentWidth / 5 * 1, 0, DocumentWidth / 5 * 1, LineHeight);

        g.DrawString(diagnosis.DiagnosisUserBase.UserSex ? "男" : "女", fontText, brushText, DocumentPadding + DocumentWidth / 5 * 1, DocumentPadding + LineJudgHeight);

        g.DrawLine(penBorder, DocumentWidth / 5 * 2, 0, DocumentWidth / 5 * 2, LineHeight);

        g.DrawString(diagnosis.DiagnosisUserBase.People, fontText, brushText, DocumentPadding + DocumentWidth / 5 * 2, DocumentPadding + LineJudgHeight);

        g.DrawLine(penBorder, DocumentWidth / 5 * 3, 0, DocumentWidth / 5 * 3, LineHeight);

        g.DrawString((DateTime.Now.Year - diagnosis.DiagnosisUserBase.BirthDay.Year).ToString(), fontText, brushText, DocumentPadding + DocumentWidth / 5 * 3, DocumentPadding + LineJudgHeight);

        g.DrawLine(penBorder, DocumentWidth / 5 * 4, 0, DocumentWidth / 5 * 4, LineHeight);

        g.DrawString(diagnosis.CreateDate.ToString("yyyy年MM月dd日"), fontText, brushText, DocumentPadding + DocumentWidth / 5 * 4, DocumentPadding + LineJudgHeight);

        g.TranslateTransform(0, LineHeight);
        g.DrawLine(penBorder, 0, 0, DocumentWidth, 0);
        //
        //：
        //：
        //：
        //：
        //：
        //：
        //：
        FormatString(g, "来诊原因：", fontLabel, brushLabel, diagnosis.Reseaon, fontText, brushText, DocumentWidth, DocumentPadding, LineHeight, LineJudgHeight);
        g.TranslateTransform(0, LineHeight * 3);
        g.DrawLine(penBorder, 0, 0, DocumentWidth, 0);

        FormatString(g, "问诊：", fontLabel, brushLabel, diagnosis.Property_Ask, fontText, brushText, DocumentWidth, DocumentPadding, LineHeight, LineJudgHeight);
        g.TranslateTransform(0, LineHeight * 10);
        g.DrawLine(penBorder, 0, 0, DocumentWidth, 0);

        FormatString(g, "脉诊：", fontLabel, brushLabel, diagnosis.Property_Touch, fontText, brushText, DocumentWidth, DocumentPadding, LineHeight, LineJudgHeight);
        g.TranslateTransform(0, LineHeight * 2);
        g.DrawLine(penBorder, 0, 0, DocumentWidth, 0);

        FormatString(g, "望診：", fontLabel, brushLabel, diagnosis.Property_Look, fontText, brushText, DocumentWidth, DocumentPadding, LineHeight, LineJudgHeight);
        g.TranslateTransform(0, LineHeight * 3);
        g.DrawLine(penBorder, 0, 0, DocumentWidth, 0);

        FormatString(g, "诊断：", fontLabel, brushLabel, diagnosis.Property_Listen, fontText, brushText, DocumentWidth, DocumentPadding, LineHeight, LineJudgHeight);
        g.TranslateTransform(0, LineHeight * 3);
        g.DrawLine(penBorder, 0, 0, DocumentWidth, 0);

        if (diagnosis.DiagnosisDrugs != null && diagnosis.DiagnosisDrugs.Count > 0)
            foreach (Model.DiagnosisDrugs i in diagnosis.DiagnosisDrugs)
            {
                FormatString(g, "内调：", fontLabel, brushLabel, i.Name, fontText, brushText, DocumentWidth, DocumentPadding, LineHeight, LineJudgHeight);
            }
        g.TranslateTransform(0, LineHeight * 7);
        g.DrawLine(penBorder, 0, 0, DocumentWidth, 0);

        FormatString(g, "推拿：", fontLabel, brushLabel, diagnosis.Massage, fontText, brushText, DocumentWidth, DocumentPadding, LineHeight, LineJudgHeight);
        g.TranslateTransform(0, LineHeight * 3);
        g.DrawLine(penBorder, 0, 0, DocumentWidth, 0);

        FormatString(g, "备注：", fontLabel, brushLabel, diagnosis.Remarks, fontText, brushText, DocumentWidth, DocumentPadding, LineHeight, LineJudgHeight);




    }

    void FormatString(Graphics g, string strLabel, Font fontLabel, SolidBrush brushLabel, string strText, Font fontText, SolidBrush brushText, float documentWidth, float documentPadding, float documentLineHeight, float documentLineJudg)
    {
        //Queue<char> qu=new Queue<char>(strLabel);
        StringBuilder sb = new StringBuilder();

        g.DrawString(strLabel, fontLabel, brushLabel, documentPadding, documentPadding);
        SizeF sfLabel = g.MeasureString(strLabel, fontLabel);
        float x = sfLabel.Width, y = 0;
        x = 0; y = sfLabel.Height;
        Queue<char> qu = new Queue<char>(strText);

        while (qu.Count > 0)
        {
            char i = qu.Dequeue();
            if (g.MeasureString(sb.ToString() + i.ToString(), fontText).Width + documentPadding * 2 + sfLabel.Width > documentWidth)
            {
                g.DrawString(sb.ToString(), fontText, brushText, x, y + documentPadding + documentLineJudg);
                sb = new StringBuilder().Append(i);
                x = 0;
                y += documentLineHeight;
                break;
            }
            sb.Append(i);
        }
        while (qu.Count > 0)
        {
            char i = qu.Dequeue();
            if (g.MeasureString(sb.ToString() + i.ToString(), fontText).Width + documentPadding * 2 > documentWidth)
            {
                g.DrawString(sb.ToString(), fontText, brushText, x + documentPadding, y + documentPadding + documentLineJudg);
                x = 0;
                y += documentLineHeight;
                sb = new StringBuilder();
            }
            sb.Append(i);
        }
        if (sb != null && sb.Length > 0)
        {
            g.DrawString(sb.ToString(), fontText, brushText, x + documentPadding, y + documentPadding + documentLineJudg);
        }
    }

    PointF FormatString(string strLabel, Font fontLabel, SolidBrush brushLabel, string strText, Font fontText, SolidBrush brushText, int width, Graphics g, PointF p)
    {
        PointF ps = p;
        SizeF ms = g.MeasureString(strLabel, fontLabel);
        //p = new PointF(p.X + ms.Width, p.Y + ms.Height);
        //g.DrawString(strLabel, fontLabel, brushLabel, p);
        StringBuilder sb = new StringBuilder();

        bool FirstLine = true;


        foreach (char i in strLabel)
        {
            if (p.X + g.MeasureString(sb.ToString() + i.ToString(), fontText).Width > width)
            {
                if (FirstLine)
                {
                    FirstLine = false;
                    //g.DrawString(sb.ToString(), fontText, brushText, p);
                    p = new PointF(17, p.Y + g.MeasureString(sb.ToString(), fontText).Height);
                    sb = new StringBuilder();
                }
                else
                {
                    sb.Append(System.Environment.NewLine);
                    sb.Append(i);
                }
                break;
            }
            else
            {
                sb.Append(i);
            }
        }
        g.DrawString(sb.ToString(), fontLabel, brushLabel, p);
        if (FirstLine)
        {
            p = new PointF(p.X + g.MeasureString(sb.ToString(), fontLabel).Width, p.Y + 1);
        }
        sb = new StringBuilder();
        foreach (char i in strText)
        {
            if (p.X - 17 + g.MeasureString(sb.ToString() + i.ToString(), fontText).Width > width)
            {
                if (FirstLine)
                {
                    FirstLine = false;
                    g.DrawString(sb.ToString(), fontText, brushText, p);
                    p = new PointF(17, p.Y + g.MeasureString(" ", fontText).Height + 1);
                    sb = new StringBuilder();
                }
                else
                {
                    sb.Append(System.Environment.NewLine);
                    sb.Append(i);
                }
            }
            else
            {
                sb.Append(i);
            }
        }
        g.DrawString(sb.ToString(), fontText, brushText, p);
        //g.DrawLine(new Pen(brushText), ps, new PointF(652, p.Y));

        p = new PointF(17, p.Y + g.MeasureString(" ", fontText).Height + 1);
        //g.DrawLine(new Pen(brushText), ps, new PointF(ps.X, p.Y));
        //g.DrawLine(new Pen(brushText), new PointF(652, ps.Y), new PointF(652, p.Y));
        //g.DrawLine(new Pen(brushLabel), p, new PointF(652, p.Y));
        return p;
    }

    string AddNewLine(string Source, Graphics g, Font font, int Width)
    {
        StringBuilder sb = new StringBuilder();
        foreach (char i in Source)
        {
            SizeF s = g.MeasureString(sb.ToString(), font);
            sb.Append(i);
            if (s.Width > Width)
            {
                sb.Remove(sb.Length - 1, 1);
                sb.Append(System.Environment.NewLine);
                sb.Append(i);
            }
        }
        return sb.ToString();
    }

    void PrintWindow(Control c, Graphics g)
    {
        if (c == null || !c.Visible || g == null)
        {
            return;
        }
        if (c.Region != null)
            g.FillRegion(new SolidBrush(c.BackColor), c.Region);
        else
            g.FillRectangle(new SolidBrush(c.BackColor), new Rectangle(c.Location, c.Size));
        if (c.Text != null && c.Text.Length > 0)
        {
            g.DrawString(c.Text, c.Font, new SolidBrush(c.ForeColor), c.Location);
        }
        if (c.Controls != null && c.Controls.Count > 0)
        {
            g.TranslateTransform(c.Left, c.Top);
            foreach (Control i in c.Controls)
            {
                PrintWindow(i, g);
            }
            g.TranslateTransform(-c.Left, -c.Top);
        }
    }

    private void frmDiagnosis_Load(object sender, EventArgs e)
    {

    }
}